{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 模型容器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### nn.Sequential"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class LeNetSequetial(nn.Module):\n",
    "    def __init__(self, classes):\n",
    "        super(LeNet2, self).__init__()\n",
    "        self.features = nn.Sequential(\n",
    "            nn.Conv2d(3, 6, 5),\n",
    "            nn.ReLU(),\n",
    "            nn.AvgPool2d(2, 2),\n",
    "            nn.Conv2d(6, 16, 5),\n",
    "            nn.ReLU(),\n",
    "            nn.AvgPool2d(2, 2)\n",
    "        )\n",
    "        self.classifier = nn.Sequential(\n",
    "            nn.Linear(16*5*5, 120),\n",
    "            nn.ReLU(),\n",
    "            nn.Linear(120, 84),\n",
    "            nn.ReLU(),\n",
    "            nn.Linear(84, classes)\n",
    "        )\n",
    "\n",
    "    def forward(self, x):\n",
    "        x = self.features(x)\n",
    "        x = x.view(x.size()[0], -1)\n",
    "        x = self.classifier(x)\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ModuleList(\n",
      "  (linears): ModuleList(\n",
      "    (0-19): 20 x Linear(in_features=10, out_features=10, bias=True)\n",
      "  )\n",
      ")\n",
      "tensor([[-0.1253, -0.1615, -0.3969, -0.2678,  0.2628, -0.0124, -0.0215, -0.0008,\n",
      "         -0.1870,  0.1515],\n",
      "        [-0.1253, -0.1615, -0.3969, -0.2678,  0.2628, -0.0124, -0.0215, -0.0008,\n",
      "         -0.1870,  0.1515],\n",
      "        [-0.1253, -0.1615, -0.3969, -0.2678,  0.2628, -0.0124, -0.0215, -0.0008,\n",
      "         -0.1870,  0.1515],\n",
      "        [-0.1253, -0.1615, -0.3969, -0.2678,  0.2628, -0.0124, -0.0215, -0.0008,\n",
      "         -0.1870,  0.1515],\n",
      "        [-0.1253, -0.1615, -0.3969, -0.2678,  0.2628, -0.0124, -0.0215, -0.0008,\n",
      "         -0.1870,  0.1515],\n",
      "        [-0.1253, -0.1615, -0.3969, -0.2678,  0.2628, -0.0124, -0.0215, -0.0008,\n",
      "         -0.1870,  0.1515],\n",
      "        [-0.1253, -0.1615, -0.3969, -0.2678,  0.2628, -0.0124, -0.0215, -0.0008,\n",
      "         -0.1870,  0.1515],\n",
      "        [-0.1253, -0.1615, -0.3969, -0.2678,  0.2628, -0.0124, -0.0215, -0.0008,\n",
      "         -0.1870,  0.1515],\n",
      "        [-0.1253, -0.1615, -0.3969, -0.2678,  0.2628, -0.0124, -0.0215, -0.0008,\n",
      "         -0.1870,  0.1515],\n",
      "        [-0.1253, -0.1615, -0.3969, -0.2678,  0.2628, -0.0124, -0.0215, -0.0008,\n",
      "         -0.1870,  0.1515]], grad_fn=<AddmmBackward0>)\n"
     ]
    }
   ],
   "source": [
    "class ModuleList(nn.Module):\n",
    "    def __init__(self):\n",
    "        super(ModuleList, self).__init__()\n",
    "        self.linears = nn.ModuleList([nn.Linear(10, 10) for i in range(20)])\n",
    "\n",
    "    def forward(self, x):\n",
    "        for i, linear in enumerate(self.linears):\n",
    "            x = linear(x)\n",
    "        return x\n",
    "\n",
    "net = ModuleList()\n",
    "\n",
    "print(net)\n",
    "\n",
    "fake_data = torch.ones((10, 10))\n",
    "\n",
    "output = net(fake_data)\n",
    "\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[[[0.0000, 0.0000, 0.0000,  ..., 0.4056, 0.1909, 0.0000],\n",
      "          [0.0000, 0.0000, 0.0000,  ..., 0.0000, 0.9579, 0.4126],\n",
      "          [0.0000, 0.6611, 0.0000,  ..., 0.0000, 0.3082, 0.3409],\n",
      "          ...,\n",
      "          [0.0000, 0.3477, 0.0000,  ..., 0.1169, 0.0000, 0.1187],\n",
      "          [0.0000, 0.0000, 0.1221,  ..., 0.0000, 0.0000, 0.1232],\n",
      "          [0.0000, 0.1271, 0.0539,  ..., 0.0000, 0.0000, 0.0387]],\n",
      "\n",
      "         [[0.0000, 1.1290, 0.0000,  ..., 0.0983, 0.0000, 0.4820],\n",
      "          [1.2758, 0.0000, 0.0000,  ..., 0.0637, 0.3755, 0.0000],\n",
      "          [0.0000, 0.0000, 0.0000,  ..., 0.0236, 0.9434, 0.0000],\n",
      "          ...,\n",
      "          [0.0000, 0.2883, 0.0000,  ..., 0.6844, 0.6549, 0.0085],\n",
      "          [0.0000, 0.2142, 0.7710,  ..., 0.2872, 0.5901, 0.1529],\n",
      "          [0.6774, 0.0000, 0.6228,  ..., 0.4654, 0.0000, 0.7522]],\n",
      "\n",
      "         [[0.0066, 0.5943, 0.1602,  ..., 0.0000, 0.0000, 0.0000],\n",
      "          [0.3983, 0.0000, 0.1716,  ..., 0.2854, 0.8466, 1.0427],\n",
      "          [0.0000, 0.2751, 0.0000,  ..., 0.2906, 0.3807, 0.0000],\n",
      "          ...,\n",
      "          [0.0000, 0.0000, 0.0000,  ..., 0.0000, 0.2106, 0.0000],\n",
      "          [0.0000, 0.0000, 0.2420,  ..., 0.1524, 0.0106, 0.0000],\n",
      "          [0.6415, 0.0000, 0.0000,  ..., 0.2389, 0.5732, 0.0000]],\n",
      "\n",
      "         ...,\n",
      "\n",
      "         [[0.1676, 0.2590, 0.0689,  ..., 0.5262, 1.5134, 0.0000],\n",
      "          [0.0000, 0.5581, 0.0000,  ..., 0.2192, 0.0000, 0.0000],\n",
      "          [0.0116, 0.9918, 0.0000,  ..., 0.1061, 0.0000, 0.0840],\n",
      "          ...,\n",
      "          [0.0000, 0.2705, 0.0310,  ..., 0.0000, 0.5824, 0.0000],\n",
      "          [0.0000, 1.1943, 0.2025,  ..., 0.8522, 1.4341, 0.0000],\n",
      "          [0.0000, 0.0000, 0.8231,  ..., 0.1877, 0.0000, 0.0971]],\n",
      "\n",
      "         [[0.0000, 0.4759, 0.0000,  ..., 0.3328, 0.4141, 0.5377],\n",
      "          [0.0000, 1.6866, 0.0000,  ..., 0.0000, 0.6113, 0.0000],\n",
      "          [0.0000, 0.6076, 0.0000,  ..., 0.0000, 0.0000, 0.0000],\n",
      "          ...,\n",
      "          [0.1464, 0.9910, 0.0000,  ..., 0.7905, 0.1907, 0.0000],\n",
      "          [0.0000, 0.0000, 0.7109,  ..., 0.0000, 0.5656, 0.0000],\n",
      "          [0.0000, 0.5244, 0.0000,  ..., 0.0000, 0.0861, 0.1314]],\n",
      "\n",
      "         [[0.0000, 0.0000, 0.4067,  ..., 0.0000, 0.0000, 0.1036],\n",
      "          [0.8047, 0.0000, 0.0000,  ..., 0.1092, 0.0000, 0.0000],\n",
      "          [0.0000, 0.0000, 0.0000,  ..., 0.0000, 0.1087, 0.4356],\n",
      "          ...,\n",
      "          [0.1711, 0.0000, 0.0000,  ..., 0.4288, 0.1686, 0.7484],\n",
      "          [0.0000, 0.0000, 0.3742,  ..., 0.0623, 0.0000, 1.1138],\n",
      "          [0.0000, 0.0000, 0.0000,  ..., 0.7277, 0.0000, 0.0000]]],\n",
      "\n",
      "\n",
      "        [[[1.1134, 0.9558, 0.0000,  ..., 0.1537, 0.0508, 0.5732],\n",
      "          [1.2472, 0.0000, 0.3626,  ..., 0.0000, 0.0000, 0.0888],\n",
      "          [0.1856, 0.2331, 0.0000,  ..., 0.0000, 0.0000, 0.0000],\n",
      "          ...,\n",
      "          [0.0000, 0.0000, 0.0000,  ..., 0.3479, 0.0000, 0.0000],\n",
      "          [1.2288, 0.0000, 0.0000,  ..., 0.0000, 0.3936, 0.1029],\n",
      "          [0.0000, 0.5020, 0.1062,  ..., 0.3310, 0.0443, 0.0000]],\n",
      "\n",
      "         [[0.1053, 0.0000, 0.2898,  ..., 0.0000, 0.2809, 0.8351],\n",
      "          [0.0000, 0.8324, 0.0000,  ..., 0.5502, 0.0660, 0.0000],\n",
      "          [0.0000, 0.0000, 0.0874,  ..., 0.0000, 0.4569, 0.0000],\n",
      "          ...,\n",
      "          [1.0403, 0.0000, 0.2898,  ..., 0.1608, 0.2811, 0.0000],\n",
      "          [0.5080, 0.0000, 0.7509,  ..., 0.2296, 0.0000, 0.0000],\n",
      "          [0.7556, 0.0000, 0.0813,  ..., 0.4025, 0.0000, 0.1665]],\n",
      "\n",
      "         [[0.0000, 0.0000, 0.0000,  ..., 0.0000, 0.0000, 0.0666],\n",
      "          [1.4503, 0.0669, 0.7807,  ..., 0.0783, 0.0000, 0.0000],\n",
      "          [0.6118, 0.2631, 0.0000,  ..., 0.0000, 0.0000, 0.0000],\n",
      "          ...,\n",
      "          [0.0000, 0.0000, 0.0000,  ..., 0.0000, 0.0889, 0.9793],\n",
      "          [0.0000, 0.0000, 0.6187,  ..., 0.9513, 0.4451, 0.0000],\n",
      "          [0.5710, 0.2103, 0.3355,  ..., 0.0000, 0.4234, 0.0000]],\n",
      "\n",
      "         ...,\n",
      "\n",
      "         [[0.0000, 0.0000, 0.1620,  ..., 0.0000, 0.0475, 0.2021],\n",
      "          [0.0000, 0.2248, 0.0959,  ..., 0.0000, 0.0846, 0.1228],\n",
      "          [0.7523, 0.6316, 0.0000,  ..., 0.3838, 0.0000, 0.0000],\n",
      "          ...,\n",
      "          [0.0000, 1.0404, 0.3427,  ..., 0.2050, 0.0000, 0.0000],\n",
      "          [0.0000, 0.0000, 0.0000,  ..., 0.3094, 1.1213, 0.1421],\n",
      "          [0.0000, 0.3921, 0.4036,  ..., 0.1327, 0.1496, 0.0000]],\n",
      "\n",
      "         [[0.9972, 0.4157, 0.0000,  ..., 0.4907, 0.0000, 0.0000],\n",
      "          [0.0000, 0.0000, 0.2965,  ..., 0.0000, 0.2060, 0.6229],\n",
      "          [0.1113, 0.0000, 0.7038,  ..., 0.0000, 0.0000, 0.0000],\n",
      "          ...,\n",
      "          [0.0000, 1.0867, 0.2847,  ..., 0.0000, 0.0000, 0.0000],\n",
      "          [0.0430, 0.0000, 0.0000,  ..., 0.0000, 0.0000, 0.0000],\n",
      "          [0.8745, 0.0000, 0.0000,  ..., 0.5403, 0.2965, 0.1485]],\n",
      "\n",
      "         [[0.0000, 0.0000, 0.0000,  ..., 0.0000, 0.0000, 0.5453],\n",
      "          [0.9807, 0.4635, 0.0000,  ..., 0.8582, 0.2848, 0.3135],\n",
      "          [0.0000, 0.0000, 0.0000,  ..., 0.0000, 0.6684, 0.0000],\n",
      "          ...,\n",
      "          [0.0000, 0.0077, 0.0000,  ..., 0.8919, 0.0898, 0.2002],\n",
      "          [0.0520, 0.0000, 0.0020,  ..., 0.0000, 0.0000, 0.0000],\n",
      "          [0.2672, 0.6038, 0.0000,  ..., 1.2288, 0.5905, 0.5330]]],\n",
      "\n",
      "\n",
      "        [[[0.1673, 0.6062, 0.4868,  ..., 0.6580, 0.2945, 0.9210],\n",
      "          [0.0000, 0.2192, 0.0000,  ..., 0.0000, 0.0000, 0.0000],\n",
      "          [0.0000, 0.0000, 0.0000,  ..., 0.0000, 0.9666, 0.0000],\n",
      "          ...,\n",
      "          [0.3469, 0.7136, 0.9326,  ..., 0.7070, 1.3690, 0.0000],\n",
      "          [0.0000, 0.1611, 0.0000,  ..., 0.1436, 0.5458, 0.8666],\n",
      "          [0.0000, 0.1215, 0.8914,  ..., 0.6591, 0.0000, 0.2975]],\n",
      "\n",
      "         [[0.1418, 0.0000, 0.0000,  ..., 0.0000, 0.6269, 0.3185],\n",
      "          [0.0000, 1.0100, 0.1548,  ..., 0.4658, 0.0000, 0.0000],\n",
      "          [0.0000, 1.1714, 0.0000,  ..., 0.0000, 0.0000, 0.0000],\n",
      "          ...,\n",
      "          [0.0000, 0.0000, 0.0000,  ..., 0.4503, 0.0000, 0.0000],\n",
      "          [0.7628, 0.9032, 0.0000,  ..., 0.0000, 0.0000, 0.0000],\n",
      "          [0.0000, 0.0000, 0.6324,  ..., 0.0000, 0.0000, 0.0000]],\n",
      "\n",
      "         [[0.1870, 0.0821, 0.5506,  ..., 0.2527, 0.0000, 0.0000],\n",
      "          [0.5588, 0.0000, 0.6477,  ..., 0.0000, 0.0000, 0.0000],\n",
      "          [1.6023, 0.0000, 0.2376,  ..., 0.0000, 0.3941, 0.4572],\n",
      "          ...,\n",
      "          [0.2920, 0.0000, 0.0000,  ..., 0.4008, 0.5666, 0.3007],\n",
      "          [0.0000, 0.0000, 0.3128,  ..., 0.0000, 1.0628, 0.1435],\n",
      "          [0.0000, 0.4643, 0.1186,  ..., 0.0000, 0.1424, 0.4645]],\n",
      "\n",
      "         ...,\n",
      "\n",
      "         [[0.2722, 0.0000, 0.0000,  ..., 0.0000, 0.0000, 0.0000],\n",
      "          [0.0000, 0.0000, 0.7703,  ..., 0.7421, 0.9209, 0.0000],\n",
      "          [0.4617, 0.0000, 0.2965,  ..., 0.6497, 0.0000, 0.0000],\n",
      "          ...,\n",
      "          [0.7874, 0.0323, 0.0000,  ..., 0.0000, 0.5124, 0.5157],\n",
      "          [0.5180, 0.3915, 0.0647,  ..., 0.0000, 0.0000, 0.0000],\n",
      "          [0.0000, 0.6475, 0.0000,  ..., 0.2894, 0.1612, 0.0704]],\n",
      "\n",
      "         [[0.0297, 1.1681, 0.0000,  ..., 0.0000, 0.0000, 1.4502],\n",
      "          [0.0000, 0.0000, 0.3155,  ..., 0.0706, 0.3985, 0.0102],\n",
      "          [0.0704, 0.3253, 0.2809,  ..., 1.2180, 0.2656, 0.0000],\n",
      "          ...,\n",
      "          [0.3249, 0.4044, 0.9485,  ..., 0.0000, 0.3851, 0.0000],\n",
      "          [0.0000, 1.3052, 0.0000,  ..., 0.0000, 0.0608, 0.1690],\n",
      "          [0.0000, 0.7380, 0.4501,  ..., 1.0500, 0.0000, 0.2613]],\n",
      "\n",
      "         [[0.3988, 0.0000, 0.1976,  ..., 0.0000, 0.0000, 0.0000],\n",
      "          [0.0000, 0.2982, 0.5017,  ..., 0.3185, 0.1182, 1.0372],\n",
      "          [0.3696, 0.4950, 0.0000,  ..., 0.0000, 0.0000, 0.0515],\n",
      "          ...,\n",
      "          [0.1496, 0.0000, 0.0868,  ..., 0.5631, 1.2815, 0.0000],\n",
      "          [0.0000, 0.4969, 0.0000,  ..., 0.2428, 0.4789, 0.1489],\n",
      "          [0.0496, 0.0000, 0.9789,  ..., 0.0000, 0.1363, 0.4365]]],\n",
      "\n",
      "\n",
      "        [[[0.0000, 0.2999, 0.2151,  ..., 0.0000, 0.0000, 0.0000],\n",
      "          [0.0000, 0.0156, 0.0000,  ..., 0.0000, 1.0236, 0.3804],\n",
      "          [0.7791, 0.6128, 0.1689,  ..., 1.0409, 0.0000, 0.7349],\n",
      "          ...,\n",
      "          [0.9605, 0.0000, 0.5839,  ..., 0.0000, 0.4660, 0.0000],\n",
      "          [0.0000, 0.0000, 0.8577,  ..., 0.0000, 0.0760, 0.3114],\n",
      "          [0.0000, 0.4243, 0.3822,  ..., 0.0000, 0.6456, 0.0000]],\n",
      "\n",
      "         [[0.5264, 0.0368, 0.0000,  ..., 1.1512, 0.0000, 0.0000],\n",
      "          [0.3665, 0.0000, 0.3101,  ..., 0.1921, 0.0000, 0.0000],\n",
      "          [0.0000, 0.0000, 0.0000,  ..., 0.0000, 0.0000, 0.6346],\n",
      "          ...,\n",
      "          [0.0250, 0.3682, 0.2000,  ..., 0.9726, 0.9667, 0.0000],\n",
      "          [0.0000, 1.2055, 0.0000,  ..., 0.0713, 0.0000, 0.0094],\n",
      "          [0.0000, 1.2463, 0.0000,  ..., 0.7584, 1.1807, 0.0000]],\n",
      "\n",
      "         [[0.0000, 0.0000, 1.0038,  ..., 0.0000, 0.0000, 0.0000],\n",
      "          [0.2988, 0.0000, 0.1300,  ..., 0.0000, 0.2847, 0.4536],\n",
      "          [0.0000, 0.4158, 0.0000,  ..., 0.0710, 0.2153, 0.0000],\n",
      "          ...,\n",
      "          [0.1968, 0.0532, 0.2385,  ..., 0.0000, 0.5520, 0.0000],\n",
      "          [0.7906, 0.0000, 0.0000,  ..., 0.0000, 0.0000, 0.7152],\n",
      "          [0.0000, 0.7447, 0.0000,  ..., 0.0000, 0.0000, 0.0000]],\n",
      "\n",
      "         ...,\n",
      "\n",
      "         [[0.6086, 0.3773, 0.7110,  ..., 0.5916, 0.8403, 0.0000],\n",
      "          [0.0797, 0.0000, 0.0000,  ..., 0.0000, 0.0000, 0.7028],\n",
      "          [0.6209, 0.0365, 0.0000,  ..., 1.1797, 0.0315, 0.5037],\n",
      "          ...,\n",
      "          [0.5188, 0.3287, 0.0000,  ..., 0.0000, 0.0000, 0.0112],\n",
      "          [0.5057, 0.0000, 0.0000,  ..., 0.0000, 0.0000, 0.1554],\n",
      "          [0.0000, 0.0000, 0.0000,  ..., 0.0000, 0.0000, 0.8958]],\n",
      "\n",
      "         [[0.0000, 0.1398, 0.4872,  ..., 0.0000, 0.0000, 0.0000],\n",
      "          [0.2689, 0.4261, 0.0000,  ..., 0.0645, 0.1473, 0.0000],\n",
      "          [0.0000, 0.0000, 0.0000,  ..., 0.0134, 0.0000, 0.9648],\n",
      "          ...,\n",
      "          [0.1001, 0.0000, 0.7695,  ..., 0.0000, 0.6647, 0.0000],\n",
      "          [0.0000, 0.1107, 0.0000,  ..., 0.0000, 0.0000, 0.6340],\n",
      "          [0.0000, 0.0000, 0.0000,  ..., 0.1354, 0.7125, 0.0000]],\n",
      "\n",
      "         [[0.0000, 0.0000, 0.6281,  ..., 0.0000, 0.5073, 0.0000],\n",
      "          [0.0000, 0.5527, 0.5145,  ..., 0.0000, 0.5257, 0.0000],\n",
      "          [0.0000, 0.0000, 0.0000,  ..., 0.1517, 0.0000, 0.2647],\n",
      "          ...,\n",
      "          [0.0000, 0.0000, 0.0000,  ..., 0.5356, 0.5810, 0.4790],\n",
      "          [0.0000, 0.4047, 0.0221,  ..., 0.0910, 0.0000, 0.0000],\n",
      "          [0.0000, 0.2876, 0.1203,  ..., 0.0000, 0.5455, 0.0000]]]],\n",
      "       grad_fn=<ReluBackward0>)\n"
     ]
    }
   ],
   "source": [
    "class ModuleDict(nn.Module):\n",
    "    def __init__(self):\n",
    "        super(ModuleDict, self).__init__()\n",
    "        self.choices = nn.ModuleDict({\n",
    "            'conv': nn.Conv2d(10, 10, 3),\n",
    "            'pool': nn.MaxPool2d(3)\n",
    "        })\n",
    "\n",
    "        self.activations = nn.ModuleDict({\n",
    "            'relu': nn.ReLU(),\n",
    "            'prelu': nn.PReLU()\n",
    "        })\n",
    "\n",
    "    def forward(self, x, choice, act):\n",
    "        x = self.choices[choice](x)\n",
    "        x = self.activations[act](x)\n",
    "        return x\n",
    "\n",
    "net = ModuleDict()\n",
    "\n",
    "fake_img = torch.randn((4, 10, 32, 32))\n",
    "\n",
    "output = net(fake_img, 'conv', 'relu')\n",
    "# output = net(fake_img, 'conv', 'prelu')\n",
    "print(output)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
